package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.mapper.CategoryMapper;
import com.app.model.CategoryModel;
import com.app.model.UserModel;
import com.app.model.VillageFairModel;
import com.app.service.UserService;
import com.app.service.VillageFairService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 乡村集市
 */
@Controller
@RestController
@RequestMapping("/api")
public class VillageFairController {
    @Autowired
    private VillageFairService villageFairService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private UserService userService;

    //类型列表
    @GetMapping("/fairTypeList")
    public Result<Object> fairTypeList() {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 2);
        return Result.success(categoryMapper.selectList(queryWrapper));
    }

    //新增和编辑
    @PostMapping("/insertAndUpdateFair")
    public Result<Object> insertAndUpdateVideo(@RequestBody VillageFairModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS401.getCode(), StatusCodeEnum.STATUS401.getMsg());
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }

        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            if (model.getStatus() == null) {
                model.setStatus(0);
            }
            villageFairService.update(model);
        } else {
            model.setUserId(user.getId());
            model.setPhone(user.getPhone());
            model.setCreateTime(CommonHelp.getNowTimestamp());
            villageFairService.insert(model);
        }
        return Result.success("操作成功");
    }


    //列表
    @GetMapping("/getFairList")
    public Result<Object> getVideoList(Integer page, Integer limit, String keyword, Integer type, HttpServletRequest request, Integer township) {
        Double longitude = null;
        Double latitude = null;
        if (request.getHeader("longitude") != null) {
            longitude = Double.parseDouble(request.getHeader("longitude"));
        }
        if (request.getHeader("latitude") != null) {
            latitude = Double.parseDouble(request.getHeader("latitude"));
        }
        return Result.success(villageFairService.selectList(page, limit, keyword, type, longitude, latitude, township));
    }

    //删除
    @GetMapping("/deleteFair")
    public Result<Object> deleteVideo(Integer id) {
        villageFairService.deleteById(id);
        return Result.success("操作成功");
    }

    //查看详情
    @GetMapping("/getFairInfo")
    public Result<Object> getVideoInfo(Integer id) {
        return Result.success(villageFairService.selectById(id));
    }

}
